package com.javaweb.common;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.util.Set;
import java.util.UUID;

/**
 * @author Ping
 * @date 2022/9/28 13:18
 **/
public class FileUploadTool {
    public static final Set<String> MIME_TYPE =
            Set.of("image/pjpeg", "application/pdf", "application/msword", "image/jpeg",
                    "image/x-png", "image/tiff", "application/vnd.ms-excel", "application/zip",
                    "image/bmp", "image/x-bitmap", "image/x-pixmap", "image/jpg",
                    "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" /*xlsx*/
                    , "application/x-rar-compressed", "application/rtf", "application/x-tika-ooxml",/*xls*/
                    "application/vnd.openxmlformats-officedocument.spreadsheetml.template",/*xltx*/
                    "application/vnd.ms-spreadsheetml",/*excel*/"application/vnd.ms-xpsdocument",/*xps*/
                    "application/x-bplist"/*pdf*/, "application/x-mswrite",/*Ms Write*/
                    "application/vnd.ms-word.document.macroenabled.12"/*docm*/, "application/x-tika-msoffice"/*pdf*/,
                    "application/vnd.openxmlformats-officedocument.wordprocessingml.document", /*docx*/
                    "application/vnd.openxmlformats-officedocument.wordprocessingml.template", /*dotx*/
                    "application/vnd.ms-powerpoint",/*ppt*/"vnd.ms-project",/*Microsoft Project*/
                    "application/vnd.visio"/*Visio*/, "image/svg+xml",/*svg*/"application/vnd.ms-works",/*wps*/
                    "application/vnd.openxmlformats-officedocument.presentationml.slideshow",/*ppsx*/
                    "application/vnd.openxmlformats-officedocument.presentationml.presentation"/*pptx*/,
                    "application/vnd.openxmlformats-officedocument.presentationml.template"
                    , "application/x-7z-compressed");
    public static boolean isImage(String mimeType) {
        return MIME_TYPE.contains(mimeType) && mimeType.startsWith("image") ;
    }

    public static boolean isDoc(String mimeType, String fileName) {
        return MIME_TYPE.contains(mimeType) && (fileName.endsWith("doc") || fileName.endsWith("docx")) ;
    }

    public static boolean isPdf(String mimeType, String fileName) {
        return MIME_TYPE.contains(mimeType) && (fileName.endsWith("pdf")) ;
    }

    /**
     * generate a new file name based on UUID.
     * @param
     * @param orginName
     * @return
     */
    public static String newFileName(String orginName) {
        return  UUID.randomUUID() + "_" + orginName ;
    }

    /**
     * get upload dir from web.xml
     * @param
     * @return
     */
    public static String getAbsoluteUploadDir(HttpServletRequest req) {
        ServletContext servletContext = req.getServletContext() ;
        String uploadDir = servletContext.getInitParameter("uploadDir") ;
        return servletContext.getRealPath("/") + uploadDir ;

    }
    public static String getUploadDir(HttpServletRequest req) {
        return req.getServletContext().getInitParameter("upload-dir") ;
    }

}
